var fs = require('fs');
var express = require('express');
var serveStatic = require('serve-static');
var path = require('path');
var morgan = require('morgan');
var healthChecker = require('sc-framework-health-check');

var globall = require('glob-all');

module.exports.run = function (worker) {
    console.log('   >> Worker PID:', process.pid);
    var environment = worker.options.environment;

    var app = express();

    var httpServer = worker.httpServer;
    var scServer = worker.scServer;

    if (environment == 'dev') {
        // Log every HTTP request. See https://github.com/expressjs/morgan for other
        // available formats.
        app.use(morgan('dev'));
    }
    app.use(serveStatic(path.resolve(__dirname, 'public')));

    // Add GET /health-check express route
    healthChecker.attach(worker, app);

    httpServer.on('request', app);

    /*
     In here we handle our incoming realtime connections and listen for events.
     */
    scServer.on('connection', function (socket) {
        var workers = globall.sync('./workers/**/*.js');
        for (var worker of workers) {
            require(worker)(socket, scServer);
        }
    });
};
